package com.szbber.medical.ui.activity

import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.szbber.commen.base.BaseActivity
import com.szbber.medical.R
import kotlinx.android.synthetic.main.actionbar_root_layout.*
import kotlinx.android.synthetic.main.activity_setting.*
import java.util.ArrayList
import com.szbber.commen.base.BaseFragmentPageAdapter
import com.szbber.commen.base.BaseRoutePath
import com.szbber.commen.bean.SettingItemBean
import com.szbber.medical.ui.fragment.setting.*
@Route(path = BaseRoutePath.app_setting)
class SettingActivity : BaseActivity(),
    View.OnClickListener {

    private var settingSettingItemBean = mutableListOf<SettingItemBean >()
    private val mFragments: ArrayList<Fragment> = ArrayList()
    private var myAdapter: BaseFragmentPageAdapter? = null

    override fun initLayout(): Int = R.layout.activity_setting
//    override fun initVM(): SettingViewModel =  ViewModelProvider(this).get(SettingViewModel::class.java)

    override fun initView() {
        rlBack.setOnClickListener(this)
        mFragments.clear()
        settingSettingItemBean.clear()
        settingSettingItemBean.add(SettingItemBean(0, "端口设置", true))
        settingSettingItemBean.add(SettingItemBean(1, "护理等级", false))
        settingSettingItemBean.add(SettingItemBean(2, "护理标签", false))
        settingSettingItemBean.add(SettingItemBean(3, "系统老化测试", false))
        settingSettingItemBean.add(SettingItemBean(4, "功能测试模式", false))
        settingSettingItemBean.add(SettingItemBean(5, "系统升级", false))

        mFragments.add(PortSettingFragment.getInstance())
        mFragments.add(NurseLevelSettingFragment.getInstance())
        mFragments.add(NurseLabelSettingFragment.getInstance())
        mFragments.add(DeviceSettingFragment.getInstance())
        mFragments.add(ModuleTestFragment.getInstance())
        mFragments.add(SystemUpgradeFragment.getInstance())

        val  adapter = object : BaseQuickAdapter<SettingItemBean, BaseViewHolder>(
            R.layout.item_setting_item,
            settingSettingItemBean
        ) {
            override fun convert(holder: BaseViewHolder, item: SettingItemBean) {
                holder.apply {
                    setText(R.id.tvName, item.name)
                    val icon = getView<ImageView>(R.id.icon)
                    val rlItem = getView<RelativeLayout>(R.id.rlItem)
                    val tvName = getView<TextView>(R.id.tvName)
                    tvName.text = item.name
                    if (item.click==true ) {
                        rlItem.setBackgroundColor(this@SettingActivity.resources.getColor(R.color.color_00c7ff))
                        //                     icon.setImageResource(iconListN[item.id])
                        tvName.setTextColor(this@SettingActivity.resources.getColor(R.color.white))
                    } else {
                        rlItem.setBackgroundColor(this@SettingActivity.resources.getColor(R.color.translucent))
                        //                     icon.setImageResource(iconList[item.id])
                        tvName.setTextColor(this@SettingActivity.resources.getColor(R.color.color_333333))
                    }
                }
            }
        }
        myAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, null)
        vpSetting.adapter = myAdapter
        vpSetting.currentItem = 0

        mRecyclerView.adapter = adapter
        val manager = LinearLayoutManager(this)
        mRecyclerView.layoutManager = manager
        adapter.setOnItemClickListener { adapter, view, position ->
            settingSettingItemBean.forEachIndexed { index, SettingItemBean ->
                if(position == index) vpSetting.currentItem = index
                SettingItemBean.click = position == index
            }
            adapter.notifyDataSetChanged()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBack -> {
                finish()
            }
        }
    }

}